package operateBusiness;

import models.MyConfig;
import models.ReturnJson;
import models.TestServerBusinessImpl;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2023/12/4.
 * update by yangxianming on 2024/6/21
 */
public class RestartSeleniumServer implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        String seleniumServerPath = myConfig.getAppPath() + "resource\\selenium-ie-server";
        String localIP = myConfig.getPropertiesConfig().getProperties().getProperty("localIP");
        ReturnJson returnJson = new ReturnJson();
        String serverSwitch = request.getParameter("serverSwitch");
        System.out.println("serverSwitch:" + serverSwitch);
        System.out.println("seleniumServerPath:" + seleniumServerPath);

        //http://localhost:1988/operate/restartSeleniumServer?serverSwitch=on
        //http://localhost:1988/operate/restartSeleniumServer?serverSwitch=off
        if (Objects.equals(serverSwitch, "on")){
            try {
                //启动selenium服务
                //java -jar selenium-server-standalone-3.141.0.jar -role hub -port 4444
                String seleniumServer = seleniumServerPath + "\\selenium-server-standalone-3.141.0.jar";
                String command = "cmd.exe /c java -jar "+seleniumServer+" -role hub -port 4444 ";
                System.out.println(command);
                Process process = Runtime.getRuntime().exec(command);

                Thread.sleep(3000);

                //java -jar [-Dwebdriver.ie.driver=指定ie驱动的位置] selenium-server-standalone-3.141.0.jar -role webdriver -hub http://10.28.138.196:4444/grid/register  -port 5560
                String ieDriver = seleniumServerPath + "\\IEDriverServer.exe ";
                command = "cmd.exe /c java -jar -Dwebdriver.ie.driver="+ieDriver+seleniumServer+" -role webdriver -hub http://"+localIP+":4444/grid/register  -port 5560";
                System.out.println(command);
                process = Runtime.getRuntime().exec(command);

                System.out.println("done");
                returnJson.setApiStatus(1);
                returnJson.setResult("done");
                return returnJson.toString();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (Objects.equals(serverSwitch, "off")){
            try {
                // 关闭所有浏览器相关进程
                Runtime.getRuntime().exec("taskkill /f /im iexplore.exe");
                Runtime.getRuntime().exec("taskkill /f /im IEDriverServer.exe");
//                Runtime.getRuntime().exec("taskkill /f /im chromedriver.exe");
                Thread.sleep(1000);
                // 关闭selenium服务
                ArrayList<String> javaDontClosePid = myConfig.getJavaPidList();
                close_selenium(javaDontClosePid);

                returnJson.setApiStatus(1);
                returnJson.setResult("done");
                return returnJson.toString();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        returnJson.setApiStatus(0);
        returnJson.setResult("can not found excelPath:");
        return returnJson.toString();
    }

    private void close_selenium(ArrayList<String> javaDontClosePid){
        try {
            Process process = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq java*\"");
            Reader reader = new InputStreamReader(process.getInputStream(), Charset.forName("GBK"));
            BufferedReader bf = new BufferedReader(reader);
            String line;
            while ((line = bf.readLine()) != null){
                System.out.println("tasklist line:" + line);
                if (line.contains("java")){
                    String[] oneList = line.split("\\s+");
                    System.out.println(oneList);
                    String oneCurrentJavaPid = oneList[1];
                    if (!javaDontClosePid.contains(oneCurrentJavaPid)){
                        String killCommand = "taskkill /F /PID " + oneCurrentJavaPid;
                        System.out.println(killCommand);
                        Runtime.getRuntime().exec(killCommand);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
